package com.pan.admin.module.loanProduct.controller;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.valid.Assert;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.ResultVO;
import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.common.exception.ServiceException;
import com.pan.admin.module.channel.entity.LoanChannel;
import com.pan.admin.module.channel.service.LoanChannelService;
import com.pan.admin.module.loanProduct.entity.po.LoanProductPlanFilterChannelPO;
import com.pan.admin.module.loanProduct.entity.vo.ReqLoanProductPlanFilterChannelVO;
import com.pan.admin.module.loanProduct.entity.vo.RespLoanProductPlanFilterChannelVO;
import com.pan.admin.module.loanProduct.service.LoanProductPlanFilterChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  机构计划屏蔽渠道
 */
@RestController
@RequestMapping("/product/plan/filter/channel")
public class LoanProductPlanFilterChannelController extends BaseController {

    @Autowired
    private LoanProductPlanFilterChannelService loanProductPlanFilterChannelService;
    @Autowired
    private LoanChannelService loanChannelService;

    /**
     *  根据计划id查询计划屏蔽渠道列表
     *
     *  @param  loanProductPlanFilterChannelPO
     *  @return ResultVO
     */
    @PreAuthorize("@ss.hasPermi('org:plan:filter:channel:list')")
    @GetMapping("/list")
    public ResultVO<Object> list(LoanProductPlanFilterChannelPO loanProductPlanFilterChannelPO) {
        Assert.isNotNullThrows(loanProductPlanFilterChannelPO, new ServiceException("非法参数"));

        Long loanProductId = loanProductPlanFilterChannelPO.getLoanProductId();
        Long loanProductPlanId = loanProductPlanFilterChannelPO.getLoanProductPlanId();
        Assert.isNotNullThrows(loanProductId, new ServiceException("非法参数2"));
        Assert.isNotNullThrows(loanProductPlanId, new ServiceException("非法参数3"));

        //  获取所有已选择的列表
        List<LoanProductPlanFilterChannelPO> listSelect = loanProductPlanFilterChannelService.list(loanProductId, loanProductPlanId);

        //  获取所有可选择渠道
        List<LoanProductPlanFilterChannelPO> listOptional = CollUtils.newArrayList();
        List<LoanChannel> listChannel = loanChannelService.list(null);
        IteratorUtils.array(listChannel, t -> {
            LoanProductPlanFilterChannelPO tempPO = new LoanProductPlanFilterChannelPO();
            tempPO.setLoanProductPlanId(loanProductPlanId);
            tempPO.setLoanChannelId(t.getId());
            tempPO.setChannelName(t.getName());
            listOptional.add(tempPO);
        });

        //  排重
        CollUtils.filter(listOptional, t -> {
            LoanProductPlanFilterChannelPO find = CollUtils.findOne(listSelect, optional -> Eq.object(t.getLoanChannelId(), optional.getLoanChannelId()));
            return ValidParam.isNotNull(find);
        });

        RespLoanProductPlanFilterChannelVO respVO = new RespLoanProductPlanFilterChannelVO();
        respVO.setListOptional(listOptional);
        respVO.setListSelected(listSelect);
        return ResultVO.suc(respVO);
    }

    /**
     *  根据计划id更新计划屏蔽渠道列表
     *
     *  @param  req
     *  @return ResultVO
     */
    @PreAuthorize("@ss.hasPermi('org:plan:filter:channel:list')")
    @Log(title = "修改计划屏蔽渠道", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultVO<Object> update(@RequestBody ReqLoanProductPlanFilterChannelVO req) {
        Assert.isNotNullThrows(req, new ServiceException("非法参数"));

        Long loanProductPlanId = req.getLoanProductPlanId();
        Assert.isNotNullThrows(loanProductPlanId, new ServiceException("非法参数2"));

        loanProductPlanFilterChannelService.update(loanProductPlanId, req.getLoanChannelIds());
        return ResultVO.suc();
    }

}
